using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 租户通知配置实体
    /// 管理租户级别的通知设置和偏好
    /// </summary>
    [Table("tenant_notification_configs")]
    public class TenantNotificationConfig : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型
        /// 如：email, sms, webhook, in_app等
        /// </summary>
        [Required]
        [StringLength(50)]
        public string NotificationType { get; set; } = string.Empty;

        /// <summary>
        /// 通知渠道
        /// 如：smtp, twilio, slack等
        /// </summary>
        [Required]
        [StringLength(50)]
        public string Channel { get; set; } = string.Empty;

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 配置参数
        /// JSON格式存储渠道特定配置
        /// </summary>
        [Column(TypeName = "text")]
        public string? Configuration { get; set; }

        /// <summary>
        /// 优先级
        /// 1-10，数字越小优先级越高
        /// </summary>
        public int Priority { get; set; } = 5;

        /// <summary>
        /// 重试次数限制
        /// </summary>
        public int MaxRetries { get; set; } = 3;

        /// <summary>
        /// 重试间隔（秒）
        /// </summary>
        public int RetryIntervalSeconds { get; set; } = 60;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 创建者ID
        /// </summary>
        [StringLength(50)]
        public string? CreatedBy { get; set; }

        /// <summary>
        /// 更新者ID
        /// </summary>
        [StringLength(50)]
        public string? UpdatedBy { get; set; }
    }

    /// <summary>
    /// 通知模板实体
    /// 存储可重用的通知模板
    /// </summary>
    [Table("notification_templates")]
    public class NotificationTemplate : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 模板名称
        /// </summary>
        [Required]
        [StringLength(200)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 模板代码
        /// 用于程序中引用的唯一标识
        /// </summary>
        [Required]
        [StringLength(100)]
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型
        /// </summary>
        [Required]
        [StringLength(50)]
        public string NotificationType { get; set; } = string.Empty;

        /// <summary>
        /// 主题模板
        /// </summary>
        [StringLength(500)]
        public string? Subject { get; set; }

        /// <summary>
        /// 内容模板
        /// 支持变量替换的模板内容
        /// </summary>
        [Column(TypeName = "text")]
        [Required]
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 模板变量
        /// JSON格式存储可用变量定义
        /// </summary>
        [Column(TypeName = "text")]
        public string? Variables { get; set; }

        /// <summary>
        /// 语言代码
        /// </summary>
        [StringLength(10)]
        public string Language { get; set; } = "zh-cn";

        /// <summary>
        /// 是否为默认模板
        /// </summary>
        public bool IsDefault { get; set; } = false;

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 版本号
        /// </summary>
        public int Version { get; set; } = 1;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 创建者ID
        /// </summary>
        [StringLength(50)]
        public string? CreatedBy { get; set; }
    }

    /// <summary>
    /// 通知记录实体
    /// 存储发送的通知历史记录
    /// </summary>
    [Table("notification_records")]
    public class NotificationRecord : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型
        /// </summary>
        [Required]
        [StringLength(50)]
        public string NotificationType { get; set; } = string.Empty;

        /// <summary>
        /// 使用的模板ID
        /// </summary>
        public long? TemplateId { get; set; }

        /// <summary>
        /// 接收者
        /// 可以是邮箱、手机号或用户ID
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Recipient { get; set; } = string.Empty;

        /// <summary>
        /// 主题
        /// </summary>
        [StringLength(500)]
        public string? Subject { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        [Column(TypeName = "text")]
        [Required]
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 发送状态
        /// pending, sent, failed, cancelled
        /// </summary>
        [Required]
        [StringLength(20)]
        public string Status { get; set; } = "pending";

        /// <summary>
        /// 发送渠道
        /// </summary>
        [StringLength(50)]
        public string? Channel { get; set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public int Priority { get; set; } = 5;

        /// <summary>
        /// 计划发送时间
        /// </summary>
        public DateTime? ScheduledAt { get; set; }

        /// <summary>
        /// 实际发送时间
        /// </summary>
        public DateTime? SentAt { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 0;

        /// <summary>
        /// 最后重试时间
        /// </summary>
        public DateTime? LastRetryAt { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        [Column(TypeName = "text")]
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 外部消息ID
        /// 第三方服务返回的消息ID
        /// </summary>
        [StringLength(255)]
        public string? ExternalMessageId { get; set; }

        /// <summary>
        /// 扩展数据
        /// </summary>
        [Column(TypeName = "text")]
        public string? Metadata { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        // 导航属性
        public virtual NotificationTemplate? Template { get; set; }
    }

    /// <summary>
    /// 通知订阅实体
    /// 用户对特定事件的通知订阅设置
    /// </summary>
    [Table("notification_subscriptions")]
    public class NotificationSubscription : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 用户ID
        /// </summary>
        [Required]
        [StringLength(50)]
        public string UserId { get; set; } = string.Empty;

        /// <summary>
        /// 事件类型
        /// 如：user_registered, order_created, system_alert等
        /// </summary>
        [Required]
        [StringLength(100)]
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型偏好
        /// 用户希望接收的通知类型
        /// </summary>
        [Required]
        [StringLength(50)]
        public string NotificationType { get; set; } = string.Empty;

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 频率限制
        /// 如：immediate, hourly, daily, weekly
        /// </summary>
        [StringLength(20)]
        public string Frequency { get; set; } = "immediate";

        /// <summary>
        /// 静音时间开始
        /// </summary>
        public TimeSpan? QuietTimeStart { get; set; }

        /// <summary>
        /// 静音时间结束
        /// </summary>
        public TimeSpan? QuietTimeEnd { get; set; }

        /// <summary>
        /// 过滤条件
        /// JSON格式存储订阅的过滤条件
        /// </summary>
        [Column(TypeName = "text")]
        public string? Filters { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
    }
}
